package com.community.common.controller;


import com.community.common.pojo.domain.Building;
import com.community.common.pojo.domain.Community;
import com.community.common.pojo.domain.House;
import com.community.common.pojo.response.CodeEnum;
import com.community.common.pojo.response.MyResponse;
import com.community.common.pojo.vo.WXPublicUserVo;
import com.community.common.service.CommunityService;
import com.community.common.service.PublicUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 对外提供的第三方api
 */
@Slf4j
@RestController
@RequestMapping("/open")
public class OpenController {

    @Resource
    private CommunityService communityService;

    @Resource
    private PictureController pictureController;

    @Resource
    private PublicUserService publicUserService;

    /**
     * 图片上传
     */
    @PostMapping(value = "uploadImg/{pictureId}")
    @ResponseBody
    public MyResponse uploadFileOss(@RequestParam("file") MultipartFile file, HttpServletRequest request, @PathVariable Long pictureId) {
        return pictureController.uploadFileOss(file, request, pictureId);
    }

    //用户新增
    @PostMapping("/saveUser")
    public MyResponse save(@Valid @RequestBody WXPublicUserVo wxPublicUserVo, BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            return MyResponse.fail(CodeEnum.PARAMETER_EXCEPTION);
        }
        return publicUserService.saveUser(wxPublicUserVo);
    }

    /**
     * 查询所有小区
     */
    @ResponseBody
    @GetMapping("/queryAllCommunity")
    public MyResponse queryAllCommunity(@RequestParam(required = false) String openId, HttpServletRequest request) {
        List<Community> communities = communityService.queryAllCommunity(openId);
        return MyResponse.success(communities);
    }

    /**
     * 查询小区下面所有楼栋
     */
    @ResponseBody
    @GetMapping("/queryAllBuilding/{xqId}")
    public MyResponse queryAllBuilding(@PathVariable String xqId, HttpServletRequest request) {
        List<Building> communities = communityService.queryAllBuilding(xqId);
        return MyResponse.success(communities);
    }

    /**
     * 查询楼栋下面所有房屋
     */
    @ResponseBody
    @GetMapping("/queryAllHouse/{ldId}")
    public MyResponse queryAllHouse(@PathVariable String ldId, HttpServletRequest request) {
        List<House> communities = communityService.queryAllHouse(ldId);
        return MyResponse.success(communities);
    }

}
